package com.andatsoft.laisim.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.service.PlayerService;
import com.andatsoft.laisim.utils.Common;

public class HeadphonesSettingActivity extends BaseActivity {

	private CheckBox mChkPlayWhenPlugged;
	private CheckBox mChkStopWhenUnPlugged;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_headphones);
		initViews();
		setupListeners();
	}

	private void initViews() {
		mChkPlayWhenPlugged = (CheckBox) findViewById(R.id.chk_box_headphones_auto_play_plugged);
		mChkStopWhenUnPlugged = (CheckBox) findViewById(R.id.chk_box_headphones_auto_stop_unplugged);

		restoreUIFromSetting();
	}

	private void restoreUIFromSetting() {
		SharedPreferences sp = getSharedPreferences(
				Common.SP_HEADPHONES_SETTING_NAME, Context.MODE_PRIVATE);

		mChkPlayWhenPlugged.setChecked(sp.getBoolean(
				Common.SP_HEADPHONES_IS_AUTO_PLAY_WHEN_PLUGGED, true));
		mChkStopWhenUnPlugged.setChecked(sp.getBoolean(
				Common.SP_HEADPHONES_IS_AUTO_STOP_WHEN_UNPLUGGED, true));

	}

	private void setupListeners() {

		mChkPlayWhenPlugged.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_HEADPHONES_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(
						Common.SP_HEADPHONES_IS_AUTO_PLAY_WHEN_PLUGGED,
						mChkPlayWhenPlugged.isChecked());
				editor.commit();

				if (mChkPlayWhenPlugged.isChecked()) {
					// enable other options
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_headphones_auto_played_plugged)),
							1.0f);
				} else {
					// disable other options
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_headphones_auto_played_plugged)),
							0.5f);

				}
				sendBroadcast(new Intent(PlayerService.INTENT_SETTING_CHANGED));
			}
		});

		mChkStopWhenUnPlugged.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// save setting value
				SharedPreferences sp = getSharedPreferences(
						Common.SP_HEADPHONES_SETTING_NAME, Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = sp.edit();
				editor.putBoolean(
						Common.SP_HEADPHONES_IS_AUTO_STOP_WHEN_UNPLUGGED,
						mChkStopWhenUnPlugged.isChecked());
				editor.commit();

				if (mChkStopWhenUnPlugged.isChecked()) {
					// enable other options
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_headphones_auto_stop_unplugged)),
							1.0f);
				} else {
					// disable other options
					setAlpha(
							((LinearLayout) findViewById(R.id.layout_setting_headphones_auto_stop_unplugged)),
							0.5f);

				}
				sendBroadcast(new Intent(PlayerService.INTENT_SETTING_CHANGED));
			}
		});

	}
}
